package com.lionel.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 714936489@qq.com
 * @date 2018/5/17 21:26
 */
@Slf4j
public class SingletonExample4 {
    private static AtomicInteger count = new AtomicInteger(0);
    private SingletonExample4() {
        log.info("私有构造函数被实例化：{}次",count.incrementAndGet());
    }
    private static SingletonExample4 instance = null;
    public static SingletonExample4 getInstance(){
        if (instance==null){
            synchronized (SingletonExample4.class){
                if(instance==null){
                    instance = new SingletonExample4();
                    log.info("实例化完成instance对象：{}",instance);
                }
            }
        }
        return instance;
    }
}
@Slf4j
class SingletonExample4Tester{
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0;i<1000;i++){
            executorService.execute(()->{
                log.info("ThreadName:{},Object:{}",
                        Thread.currentThread().getName(),SingletonExample4.getInstance());
            });
        }
        executorService.shutdown();
    }
}
